package com.acwing.partition10;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2021/12/2 15:55
 */
public class AC906区间分组 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int[][] intervals = new int[n][2];
        for (int i = 0; i < n; i++) {
            s = reader.readLine().split(" ");
            intervals[i][0] = Integer.parseInt(s[0]);
            intervals[i][1] = Integer.parseInt(s[1]);
        }
        writer.write(greedy(intervals) + "\n");
        writer.flush();
    }

    private static int greedy(int[][] intervals) {
        //将所有区间从小到大根据起点进行排序
        Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
        //小顶堆，根据区间右端点进行排序，存放一个二元组 (左端点, 右端点)，也可以只保存右端点
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o[1]));
        //遍历所有区间，判断当前区间能否放入某一个组里面（当前区间的左端点小于组的右端点），如果可以放入，则放入并更新最大右端点
        //如果没有这样的组，添加一个新的组
        for (int[] interval : intervals) {
            if (queue.isEmpty() || queue.peek()[1] >= interval[0]) {
                //不存在这样的组，也就是和目前所有组都冲突，新建一个组
                queue.add(new int[]{interval[0], interval[1]});
            } else {
                //更新堆顶元素
                int[] tuple = queue.poll();
                tuple[1] = interval[1];
                queue.add(tuple);
            }
        }
        return queue.size();
    }
}
